package com.rui.service.impl;

import com.rui.domain.Academic;
import com.rui.domain.Teacher;
import com.rui.domain.User;
import com.rui.domain.UserExample;
import com.rui.mapper.AcademicMapper;
import com.rui.mapper.MyCustomMapper;
import com.rui.mapper.TeacherMapper;
import com.rui.mapper.UserMapper;
import com.rui.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("teacherService")
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private MyCustomMapper myCustomMapper;
    @Autowired
    private AcademicMapper academicMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Teacher findTeacherByJobNo(String jobNo) {
        Teacher teacher = teacherMapper.selectByPrimaryKey(jobNo);
        return teacher;
    }

    @Override
    public boolean addTeacher(Teacher teacher, User user) {
        int row1 = teacherMapper.insert(teacher);
        int row2 = userMapper.insert(user);
        if (row1 > 0 && row2 > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<Teacher> findAllTeachersAndAcademices() {
        List<Teacher> teacherList = myCustomMapper.findAllTeachersAndAcademices();
        if (teacherList != null && teacherList.size() > 0) {
            return teacherList;
        }
        return null;
    }

    @Override
    public boolean deleteTeacher(String jobNo) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(jobNo);
        int row1 = userMapper.deleteByExample(example);
        int row2 = teacherMapper.deleteByPrimaryKey(jobNo);
        if (row1 > 0 && row2 > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTeacher(Teacher teacher) {
        int row = teacherMapper.updateByPrimaryKeySelective(teacher);
        if (row > 0) {
            return true;
        }
        return false;
    }
}
